package cn.tedu.ticket;
/**本类通过继承的方式实现多线程售票案例*/
//需求:4个窗口,模拟售票,同时售票共计100张,售完为止
public class TicketRunnable {
    public static void main(String[] args) {
        /**target是统一的目标业务对象
         * 而Thread类对象t1,t2...是多个线程对象*/
        TicketR target=new TicketR();
        Thread t1=new Thread(target);
        Thread t2=new Thread(target);
        Thread t3=new Thread(target);
        Thread t4=new Thread(target);
        t1.start();
        t2.start();
        t3.start();
        t4.start();


    }
}
class TicketR implements Runnable{
    int tickets=100;
    public void run() {
       //循环结构
       while (true){
           try {
               Thread.sleep(10);//给程序主动设置休眠,制造阻塞,暴露数据安全问题
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println(Thread.currentThread().getName()+"="+tickets--);
           if(tickets<=0)break;
       }
    }
}